#import <Foundation/Foundation.h>
#import <CoreFoundation/CoreFoundation.h>
#import <AppKit/AppKit.h>



#pragma mark - Implementation Section

// Implementation Section

@interface sBT:NSObject
{
	NSMutableDictionary *fullBMFile;
	NSMutableArray *barBookmarks;
	NSMutableArray *menuBookmarks;
	NSString *wpath;
	NSString *wfinalPath;
	NSString *rpath;
	NSString *rfinalPath;
	// OSVersion
	int osVersion;
	
	// Title KeyName
	NSString *titleKeyName;
	
}
-(NSString*) stringWithUUID;
-(void)readBMFile;
-(void)deleteAllBookmarksFrom:(NSString*)theLocation;
-(void)createSingleBookmarkAt:(NSString*)theLocation:(NSString*)theTitle:(NSString*)theURL;
-(void)createFolder:(NSString*)theLocation:(NSString*)folderTitle;
-(void)createBookmarkInFolder:(NSString*)theLocation:(NSString*)folderTitle:(NSString*)bmTitle:(NSString*)bmURL;
-(int)findLocationIndex:(NSMutableArray*)theArray:(NSString*)theLocation;
// Get OS Version
-(void)getOSVersion;

@end

@implementation sBT

-(id)init

{

	if (self) {
	// Initialize code goes here

		// Get OSVersion
		osVersion = 0;
		//printf ("Getting OS Version from INIT");
		[self getOSVersion];
		
		// Write fullBMFile to file
		wpath = [[NSString alloc] init];
		wpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Safari/"];
		
		wfinalPath = [wpath stringByAppendingPathComponent:@"Bookmarks.plist"];
		
		// read fullBMFile to file
		rpath = [[NSString alloc] init];
		rpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Safari/"];
		rfinalPath = [rpath stringByAppendingPathComponent:@"Bookmarks.plist"];
		
		//NSLog(@"WPATH: %@\n", wpath);
	
	}
	return self;
	
}



- (void)getOSVersion
{
	
	printf("Getting OS Version");
	//if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_4) {
		/* On a 10.4 - 10.4.x system */
		osVersion = 4;
		//printf("OS 10.4");
		titleKeyName = @"title";
	//} else if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_5) {
		/* On a 10.5 - 10.5.x system */
	//	osVersion = 5;
		//printf("OS 10.5");
	//	titleKeyName = @"Title";
	//} else {
		/* 10.6 or later system */
	//	osVersion = 6;
		//printf("OS 10.6");
	//	titleKeyName = @"Title";
	}
	
	//NSLog(@"OS Version: %i", osVersion);
//}


// START Generate UUID
-(NSString*) stringWithUUID {
	CFUUIDRef	uuidObj = CFUUIDCreate(nil);//create a new UUID
	//get the string representation of the UUID
	NSString	*uuidString = (NSString*)CFUUIDCreateString(nil, uuidObj);
	CFRelease(uuidObj);
	
	return [uuidString autorelease];
}

// END Generate UUID

// START Load bookmarks plist file

#pragma mark - Read the BM Plist File

-(void)readBMFile
{
	//NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Safari/"];
	//NSString *finalPath = [path stringByAppendingPathComponent:@"Bookmarks.plist"];
	//NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
	
	
	if ([[NSFileManager defaultManager] fileExistsAtPath: rfinalPath]) {
	fullBMFile = [[NSMutableDictionary dictionaryWithContentsOfFile:rfinalPath] retain];
	} else {
	// File doesn't exist
	printf("ERROR: Bookmarks file could not be found.");
	exit (1);
	}
	// print contents of bm file to screen
	//NSLog(@"Bookmarks file contents:\n%@", fullBMFile);
	
}

// END Load bookmarks plist file


// START Find Location Index
#pragma mark - Find Location Index

-(int)findLocationIndex:(NSMutableArray*)theArray:(NSString*)theLocation
{
	// Variable to hold index
	int locationIndex;
	
	
	
	
	
	// Setup variable for found folder
	int folderExists;
	folderExists = 0;
	
	//Setup variable to find index for found folder
	int bmIndexer;
	bmIndexer = 0;
	
	
	// Test for existing folder
	
	//NSLog(@"The Count is: %i", [currentBMContents count]);
	
	//Accessing an Array's contents using NSEnumerator
	NSEnumerator *enumerator = [theArray objectEnumerator];
	id obj;
	
	while ( obj = [enumerator nextObject] ) {

	// Test for Folders of bookmarks


	if (strcmp([[obj objectForKey:titleKeyName] UTF8String], [theLocation UTF8String]) == 0){

		// Folder exists add 1 to the amount of folders found
		folderExists++;

		// folder exists get the index of the folder
		locationIndex = bmIndexer;

	}

	// Add 1 to indexer
	bmIndexer++;
	}
	
	if (folderExists == 1){
		// return the location index
		//NSLog(@"Found Location %@ at index %i", theLocation, locationIndex);
		return locationIndex;
		
	} else {
		
		printf("ERROR: Unable to find the specified loctation.");
		exit(1);
		return -1;
	}
}
// END Find Location Index




#pragma mark - Delete All Bookmarks From
// START Remove bookmarks from plist file in the following location

-(void)deleteAllBookmarksFrom:(NSString*)theLocation
{

		//read in the plist file
		[self readBMFile];
		//printf("Bookmarks file loaded.\n");
	

	
	

	
	
	// Decide which to delete
	if(strcasecmp([theLocation UTF8String], "Bar") ==0 ){
		
		//setup the barBookmarks array
		int bmLocation = [self findLocationIndex:[fullBMFile objectForKey:@"Children"] :[NSString stringWithString:@"BookmarksBar"]];
		//NSLog (@"BMLocation of Bar is: %i", bmLocation);
		
		NSDictionary *tempBarBM = [[NSDictionary alloc] initWithDictionary:[[fullBMFile objectForKey:@"Children"] objectAtIndex:bmLocation]];
				
		
		if ( strcmp([[tempBarBM objectForKey:titleKeyName] UTF8String], "BookmarksBar") == 0) {

			barBookmarks = [[tempBarBM objectForKey:@"Children"] copy];
									
			// remove all Bookmarks from bar
			[[[[fullBMFile objectForKey:@"Children"] objectAtIndex:bmLocation] objectForKey:@"Children"] removeAllObjects];
			
			// Write fullBMFile to file
			//NSString *wpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Safari/"];
			//NSString *wfinalPath = [wpath stringByAppendingPathComponent:@"Bookmarks_sBookmarkTool.plist"];
			
			if (! [fullBMFile writeToFile:wfinalPath atomically: YES]){
			printf("ERROR: Could not save changes to bookmarks file.");
			exit (1);
			}
			
			// Print total bookmarks in location
			printf("\t\tTotal Bookmarks Deleted from Bar: %s\n", [[NSString stringWithFormat:@"%i",[barBookmarks count]] UTF8String]);

		}
		
		// release all the objects
		[barBookmarks release];
		[tempBarBM release];
		[fullBMFile release];
		

	} else if(strcasecmp([theLocation UTF8String], "Both") == 0 ) {
		//printf("delete all bookmarks from the Bar and the Menu.\n");
				
		//setup the barBookmarks array
		int bmLocation = [self findLocationIndex:[fullBMFile objectForKey:@"Children"] :[NSString stringWithString:@"BookmarksBar"]];
		NSDictionary *tempBarBM = [[NSDictionary alloc] initWithDictionary:[[fullBMFile objectForKey:@"Children"] objectAtIndex:bmLocation]];
				
		
		if ( strcmp([[tempBarBM objectForKey:titleKeyName] UTF8String], "BookmarksBar") == 0) {
			
			barBookmarks = [[tempBarBM objectForKey:@"Children"] copy];
					
			//printf("\t\tdeleting all bookmarks from the Bar.\n");

			// remove all Bookmarks from bar
			[[[[fullBMFile objectForKey:@"Children"] objectAtIndex:1] objectForKey:@"Children"] removeAllObjects];
			
			// Write fullBMFile to file
			//NSString *wpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Safari/"];
			//NSString *wfinalPath = [wpath stringByAppendingPathComponent:@"Bookmarks_sBookmarkTool.plist"];
			
			if (! [fullBMFile writeToFile:wfinalPath atomically: YES]){
				printf("ERROR: Could not save changes to bookmarks file");
				exit (1);
			}

			// Print total bookmarks in location
			printf("\t\tTotal Bookmarks Deleted From Bar: %s\n", [[NSString stringWithFormat:@"%i",[barBookmarks count]] UTF8String]);
			
		
		}
		
		//setup the menuBookmarks array
		int bmMenuLocation = [self findLocationIndex:[fullBMFile objectForKey:@"Children"] :[NSString stringWithString:@"BookmarksMenu"]];
		
		
		NSDictionary *tempMenuBM = [[NSDictionary alloc] initWithDictionary:[[fullBMFile objectForKey:@"Children"] objectAtIndex:bmMenuLocation]];
		
		
		
		if ( strcmp([[tempMenuBM objectForKey:titleKeyName] UTF8String], "BookmarksMenu") == 0) {
			
			menuBookmarks = [[tempMenuBM objectForKey:@"Children"] copy];
			
			
			//printf("\t\tdeleting all bookmarks from the Menu.\n");
			
			// remove all Bookmarks from bar
			[[[[fullBMFile objectForKey:@"Children"] objectAtIndex:2] objectForKey:@"Children"] removeAllObjects];
			
			// Write fullBMFile to file
			//NSString *wpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Safari/"];
			//NSString *wfinalPath = [wpath stringByAppendingPathComponent:@"Bookmarks_sBookmarkTool.plist"];
			
			if ([fullBMFile writeToFile:wfinalPath atomically: YES]) {
				
				// Print total bookmarks in location
				printf("\tTotal Bookmarks Deleted From Menu: %s\n", [[NSString stringWithFormat:@"%i",[menuBookmarks count]] UTF8String]);
				
			} else {
				printf("ERROR: Could not save changes to bookmarks file.");
				exit (1);
			}
			
			
		}
		
		
		
		
		// release all the objects
		[menuBookmarks release];
		[barBookmarks release];
		[tempBarBM release];
		[tempMenuBM release];
		[fullBMFile release];
		
		
	} else if(strcasecmp([theLocation UTF8String], "Menu") == 0 ){
	
			
		//setup the menuBookmarks array
		int bmLocation = [self findLocationIndex:[fullBMFile objectForKey:@"Children"] :[NSString stringWithString:@"BookmarksMenu"]];
		NSDictionary *tempMenuBM = [[NSDictionary alloc] initWithDictionary:[[fullBMFile objectForKey:@"Children"] objectAtIndex:bmLocation]];
		


		if ( strcmp([[tempMenuBM objectForKey:titleKeyName] UTF8String], "BookmarksMenu") == 0) {

		menuBookmarks = [[tempMenuBM objectForKey:@"Children"] copy];

		// remove all Bookmarks from bar
		[[[[fullBMFile objectForKey:@"Children"] objectAtIndex:2] objectForKey:@"Children"] removeAllObjects];

		// Write fullBMFile to file
		//NSString *wpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Safari/"];
		//NSString *wfinalPath = [wpath stringByAppendingPathComponent:@"Bookmarks_sBookmarkTool.plist"];

		if ([fullBMFile writeToFile:wfinalPath atomically: YES]) {
			
			// Print total bookmarks in location
			printf("\tTotal Bookmarks Deleted From Menu: %s\n", [[NSString stringWithFormat:@"%i",[menuBookmarks count]] UTF8String]);
			
		} else {
			printf("ERROR: Could not save changes to bookmarks file.");
			exit (1);
		}
		

		}

		// release all the objects
		[menuBookmarks release];
		[tempMenuBM release];
		[fullBMFile release];
		
	}

}
// END Remove bookmarks from plist file in the following location


// START Create single bookmark in the following location

-(void)createSingleBookmarkAt:(NSString*)theLocation:(NSString*)theTitle:(NSString*)theURL
{	
	//read in the plist file
	[self readBMFile];
	
	
	
	// Decide which to location to create single bookmark

	if(strcasecmp([theLocation UTF8String], "Bar") == 0 ){
	#pragma mark -	-	Create Single in Bar
		
		//setup the barBookmarks array
		int bmLocation = [self findLocationIndex:[fullBMFile objectForKey:@"Children"] :[NSString stringWithString:@"BookmarksBar"]];
		NSDictionary *tempBarBM = [[NSDictionary alloc] initWithDictionary:[[fullBMFile objectForKey:@"Children"] objectAtIndex:bmLocation]];
			
		
		if ( strcmp([[tempBarBM objectForKey:titleKeyName] UTF8String], "BookmarksBar") == 0) {
			// START Setup a bookmarkDictionary
			
					// Setup Main BM Dictionary
					NSMutableDictionary *bmInfo = [[NSMutableDictionary alloc] init];
					
					//Setup BM Title Dictionary
					NSMutableDictionary *bmTitle = [[NSMutableDictionary alloc] init];
					[bmTitle setObject:[NSString stringWithFormat:@"%@",theTitle] forKey:titleKeyName];
			
					// Add title to bm Dictionary
					[bmInfo setObject:bmTitle forKey:@"URIDictionary"];
					
					// Add URL to bm Dictionary
					[bmInfo setObject:[NSString stringWithFormat:@"%@",theURL] forKey:@"URLString"];
		
			
					// Add WebBookmarkType String
					[bmInfo setObject:[NSString stringWithString:@"WebBookmarkTypeLeaf"] forKey:@"WebBookmarkType"];
					
					// Add UUID for the BM
					[bmInfo setObject:[NSString stringWithFormat:@"%@",[self stringWithUUID]] forKey:@"WebBookmarkUUID"];
				
					
			// END Setup a bookmarkDictionary
						
			
			// Add BM to File
			[[[[fullBMFile objectForKey:@"Children"] objectAtIndex:bmLocation] objectForKey:@"Children"] insertObject:[bmInfo mutableCopy] atIndex:0];
			

			// Write fullBMFile to file
			//NSString *wpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Safari/"];
			//NSString *wfinalPath = [wpath stringByAppendingPathComponent:@"Bookmarks_sBookmarkTool.plist"];
			
			if (! [fullBMFile writeToFile:wfinalPath atomically: YES]) {
				printf("\tERROR: Bookmark was not added to the Bookmark Bar.\n\n");
				exit (1);
			}
			
			// release all the objects
			[bmInfo release];
			[bmTitle release];
			[fullBMFile release];
			[tempBarBM release];

		}
		

		
	} else if(strcasecmp([theLocation UTF8String], "Menu") == 0 ){
#pragma mark -	-	Create Single in Menu
		
		//setup the menuBookmarks dictionary
		int bmLocation = [self findLocationIndex:[fullBMFile objectForKey:@"Children"] :[NSString stringWithString:@"BookmarksMenu"]];
		NSDictionary *tempMenuBM = [[NSDictionary alloc] initWithDictionary:[[fullBMFile objectForKey:@"Children"] objectAtIndex:bmLocation]];
		
		
		if ( strcmp([[tempMenuBM objectForKey:titleKeyName] UTF8String], "BookmarksMenu") == 0) {
			
			// START Setup a bookmarkDictionary
			
			// Setup Main BM Dictionary
			NSMutableDictionary *bmInfo = [[NSMutableDictionary alloc] init];
			
			//Setup BM Title Dictionary
			NSMutableDictionary *bmTitle = [[NSMutableDictionary alloc] init];
			[bmTitle setObject:[NSString stringWithFormat:@"%@",theTitle] forKey: titleKeyName];
			
			// Add title to bm Dictionary
			[bmInfo setObject:bmTitle forKey:@"URIDictionary"];
			
			// Add URL to bm Dictionary
			[bmInfo setObject:[NSString stringWithFormat:@"%@",theURL] forKey:@"URLString"];
			
			
			// Add WebBookmarkType String
			[bmInfo setObject:[NSString stringWithString:@"WebBookmarkTypeLeaf"] forKey:@"WebBookmarkType"];
			
			// Add UUID for the BM
			[bmInfo setObject:[NSString stringWithFormat:@"%@",[self stringWithUUID]] forKey:@"WebBookmarkUUID"];
			
			
			// END Setup a bookmarkDictionary
			
			
			// Add BM to File
			[[[[fullBMFile objectForKey:@"Children"] objectAtIndex:bmLocation] objectForKey:@"Children"] insertObject:[bmInfo mutableCopy] atIndex:0];
			
			
			// Write fullBMFile to file
			//NSString *wpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Safari/"];
			//NSString *wfinalPath = [wpath stringByAppendingPathComponent:@"Bookmarks_sBookmarkTool.plist"];
			
			if ( ! [fullBMFile writeToFile:wfinalPath atomically: YES]) {

				printf("\tERROR: Bookmark was not added to the Bookmark Menu.\n\n");	
				exit (1);
			}
			
			// release all the objects
			[bmInfo release];
			[bmTitle release];
			[fullBMFile release];
			[tempMenuBM release];
			
		}
		
		
	}
	
	
}
// START Create single bookmark in the following location


// START Create single empty folder in following location
#pragma mark - Create Empty Folder

-(void)createFolder:(NSString*)theLocation:(NSString*)folderTitle
{
	//read in the plist file
	[self readBMFile];
	
	// Decide which to location to create single bookmark
	
	if(strcasecmp([theLocation UTF8String], "Bar") == 0 ){
#pragma mark -	-	Create Empty Folder in Bar
		
		//setup the barBookmarks array
		int bmLocation = [self findLocationIndex:[fullBMFile objectForKey:@"Children"] :[NSString stringWithString:@"BookmarksBar"]];
		NSDictionary *tempBarBM = [[NSDictionary alloc] initWithDictionary:[[fullBMFile objectForKey:@"Children"] objectAtIndex:bmLocation]];

		if ( strcmp([[tempBarBM objectForKey:titleKeyName] UTF8String], "BookmarksBar") == 0) {
			
			
			// Setup variable for found folder
			int folderExists;
			folderExists = 0;

			// Test for existing folder
			NSMutableArray *currentBMContents = [[NSMutableArray alloc] initWithArray:[tempBarBM objectForKey:@"Children"]];
						
			//Accessing an Array's contents using NSEnumerator
			NSEnumerator *enumerator = [currentBMContents objectEnumerator];
			id obj;
			
			while ( obj = [enumerator nextObject] ) {
				
				// Test for Folders of bookmarks
				if (strcmp([[obj objectForKey:@"WebBookmarkType"] UTF8String], "WebBookmarkTypeList") == 0){
					
					// Test for a folder with the same name as the one given
					if (strcmp([[obj objectForKey:titleKeyName] UTF8String], [folderTitle UTF8String]) == 0){
						folderExists++;
						//NSLog( @"%@", obj);
					}
				}
			}
			
			if (folderExists == 0) {				
				
				// START Setup a bookmarkDictionary
				
				// Setup Main BM Dictionary
				NSMutableDictionary *folderInfo = [[NSMutableDictionary alloc] init];
				
				
				// Add Children Array to BM Dictionary
				NSArray *theChildren = [[NSArray alloc] init];
				[folderInfo setObject:theChildren forKey:@"Children"];
				
				
				// Add URL to bm Dictionary
				[folderInfo setObject:[NSString stringWithFormat:@"%@",folderTitle] forKey:titleKeyName];
				
				
				// Add WebBookmarkType String
				[folderInfo setObject:[NSString stringWithString:@"WebBookmarkTypeList"] forKey:@"WebBookmarkType"];
				
				// Add UUID for the BM
				[folderInfo setObject:[NSString stringWithFormat:@"%@",[self stringWithUUID]] forKey:@"WebBookmarkUUID"];
				
				
				// END Setup a bookmarkDictionary
				
				
				// Add BM to File
				[[[[fullBMFile objectForKey:@"Children"] objectAtIndex:bmLocation] objectForKey:@"Children"] insertObject:[folderInfo mutableCopy] atIndex:0];
				
				
				// Write fullBMFile to file
				//NSString *wpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Safari/"];
				//NSString *wfinalPath = [wpath stringByAppendingPathComponent:@"Bookmarks_sBookmarkTool.plist"];
				
				if (! [fullBMFile writeToFile:wfinalPath atomically: YES]) {
					printf("\tERROR: Bookmark was not added to the Bookmark Bar.\n\n");
					exit (1);
				}
				
				// release all the objects
				[folderInfo release];

			} else {
				printf("\nERROR:\tFound %s folder(s) with the same name. No folder was created.\n", [[NSString stringWithFormat:@"%i", folderExists] UTF8String]);
			}
			
			// release all the objects
			[currentBMContents release];
			[fullBMFile release];
			[tempBarBM release];
		}
		
		
		
	} else if (strcasecmp([theLocation UTF8String], "Menu") == 0 ){

#pragma mark -	-	Create Empty Folder in Menu
		
		//setup the menuBookmarks array
		int bmLocation = [self findLocationIndex:[fullBMFile objectForKey:@"Children"] :[NSString stringWithString:@"BookmarksMenu"]];
		NSDictionary *tempMenuBM = [[NSDictionary alloc] initWithDictionary:[[fullBMFile objectForKey:@"Children"] objectAtIndex:bmLocation]];
		
		
		if ( strcmp([[tempMenuBM objectForKey:titleKeyName] UTF8String], "BookmarksMenu") == 0) {
			
			
			// Setup variable for found folder
			int folderExists;
			folderExists = 0;
			
			// Test for existing folder
			NSMutableArray *currentBMContents = [[NSMutableArray alloc] initWithArray:[tempMenuBM objectForKey:@"Children"]];
			
			//Accessing an Array's contents using NSEnumerator
			NSEnumerator *enumerator = [currentBMContents objectEnumerator];
			id obj;
			
			while ( obj = [enumerator nextObject] ) {
				
				// Test for Folders of bookmarks
				if (strcmp([[obj objectForKey:@"WebBookmarkType"] UTF8String], "WebBookmarkTypeList") == 0){
					
					// Test for a folder with the same name as the one given
					if (strcmp([[obj objectForKey:titleKeyName] UTF8String], [folderTitle UTF8String]) == 0){
						folderExists++;
						//NSLog( @"%@", obj);
					}
				}
			}
			
			// No Existing folder found
			if (folderExists == 0) {				
				
				// START Setup a bookmarkDictionary
				
				// Setup Main BM Dictionary
				NSMutableDictionary *folderInfo = [[NSMutableDictionary alloc] init];
				
				
				// Add Children Array to BM Dictionary
				NSArray *theChildren = [[NSArray alloc] init];
				[folderInfo setObject:theChildren forKey:@"Children"];
				
				
				// Add URL to bm Dictionary
				[folderInfo setObject:[NSString stringWithFormat:@"%@",folderTitle] forKey:titleKeyName];
				
				
				// Add WebBookmarkType String
				[folderInfo setObject:[NSString stringWithString:@"WebBookmarkTypeList"] forKey:@"WebBookmarkType"];
				
				// Add UUID for the BM
				[folderInfo setObject:[NSString stringWithFormat:@"%@",[self stringWithUUID]] forKey:@"WebBookmarkUUID"];
				
				
				// END Setup a bookmarkDictionary
				
				
				// Add BM to File
				[[[[fullBMFile objectForKey:@"Children"] objectAtIndex:bmLocation] objectForKey:@"Children"] insertObject:[folderInfo mutableCopy] atIndex:0];
				
				
				// Write fullBMFile to file
				//NSString *wpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Safari/"];
				//NSString *wfinalPath = [wpath stringByAppendingPathComponent:@"Bookmarks_sBookmarkTool.plist"];
				
				if (! [fullBMFile writeToFile:wfinalPath atomically: YES]) {
					printf("\tERROR: Bookmark was not added to the Bookmark Bar.\n\n");	
					exit (1);
				}
				
				// release all the objects
				[folderInfo release];
				
			} else {
				printf("\nERROR:\tFound %s folder(s) with the same name. No folder was created.\n", [[NSString stringWithFormat:@"%i", folderExists] UTF8String]);
			}
			
			// release all the objects
			[currentBMContents release];
			[fullBMFile release];
			[tempMenuBM release];
		}
		
		
	}
	// Done
}
// END Create single empty folder in following location

// START Create single bookmark in the given folder at the given location

-(void)createBookmarkInFolder:(NSString*)theLocation:(NSString*)folderTitle:(NSString*)bmTitle:(NSString*)bmURL
{
	//read in the plist file
	[self readBMFile];
	
	//NSLog(@"theLocation: %@", theLocation);
	//NSLog(@"folderTitle: %@", folderTitle);
	
	
	// Decide which to location to create single bookmark
	
	if(strcasecmp([theLocation UTF8String], "Bar") == 0 ){
#pragma mark -	-	Create Bookmark in the Folder in the Bar
		
		//setup the barBookmarks array
		int bmLocation = [self findLocationIndex:[fullBMFile objectForKey:@"Children"] :[NSString stringWithString:@"BookmarksBar"]];		
		NSDictionary *tempBarBM = [[NSDictionary alloc] initWithDictionary:[[fullBMFile objectForKey:@"Children"] objectAtIndex:bmLocation]];
		
		
		if ( strcmp([[tempBarBM objectForKey:titleKeyName] UTF8String], "BookmarksBar") == 0) {
			
			
			// Setup variable for found folder
			int folderExists;
			folderExists = 0;
			
			//Setup variable to find index for found folder
			int bmIndexer;
			int folderExistsIndex;
			bmIndexer = 0;

			
			// Test for existing folder
			NSMutableArray *currentBMContents = [[NSMutableArray alloc] initWithArray:[[tempBarBM objectForKey:@"Children"] copy]];
			
			//NSLog(@"The Count is: %i", [currentBMContents count]);
			
			//Accessing an Array's contents using NSEnumerator
			NSEnumerator *enumerator = [currentBMContents objectEnumerator];
			id obj;
			
			while ( obj = [enumerator nextObject] ) {
				
				// Test for Folders of bookmarks
				if (strcmp([[obj objectForKey:@"WebBookmarkType"] UTF8String], "WebBookmarkTypeList") == 0){
					
					// Test for a folder with the same name as the one given
					if (strcmp([[obj objectForKey:titleKeyName] UTF8String], [folderTitle UTF8String]) == 0){
						
						// Folder exists add 1 to the amount of folders found
						folderExists++;
						
						// folder exists get the index of the folder
						folderExistsIndex = bmIndexer;
						
						//NSLog( @"%@", obj);
					}
				}
				
				// Add 1 to indexer
				bmIndexer++;
			}
			
			// No Folder found, create a new one
			if (folderExists == 0) {				
				
				// START Setup a bookmarkDictionary
				
				// Setup Main BM Dictionary
				NSMutableDictionary *folderInfo = [[NSMutableDictionary alloc] init];
				
				
				// Add Children Array to BM Dictionary
				NSMutableArray *theChildren = [[NSMutableArray alloc] init];
				
				
				// START Setup a bookmarkDictionary
				
				// Setup Main BM Dictionary
				NSMutableDictionary *bmInfo = [[NSMutableDictionary alloc] init];
				
				//Setup BM Title Dictionary
				NSMutableDictionary *bmTempTitle = [[NSMutableDictionary alloc] init];
				[bmTempTitle setObject:[NSString stringWithFormat:@"%@",bmTitle] forKey: titleKeyName];
				
				// Add title to bm Dictionary
				[bmInfo setObject:bmTempTitle forKey:@"URIDictionary"];
				
				// Add URL to bm Dictionary
				[bmInfo setObject:[NSString stringWithFormat:@"%@",bmURL] forKey:@"URLString"];
				
				
				// Add WebBookmarkType String
				[bmInfo setObject:[NSString stringWithString:@"WebBookmarkTypeLeaf"] forKey:@"WebBookmarkType"];
				
				// Add UUID for the BM
				[bmInfo setObject:[NSString stringWithFormat:@"%@",[self stringWithUUID]] forKey:@"WebBookmarkUUID"];
				
				// END Setup a bookmarkDictionary
				
				// Add the URL to the children dictionary
				[theChildren insertObject:bmInfo atIndex:0];
				
				// Add the children array to the folder info
				[folderInfo setObject:theChildren forKey:@"Children"];

				// Add URL to bm Dictionary
				[folderInfo setObject:[NSString stringWithFormat:@"%@",folderTitle] forKey:titleKeyName];
				
				
				// Add WebBookmarkType String
				[folderInfo setObject:[NSString stringWithString:@"WebBookmarkTypeList"] forKey:@"WebBookmarkType"];
				
				// Add UUID for the BM
				[folderInfo setObject:[NSString stringWithFormat:@"%@",[self stringWithUUID]] forKey:@"WebBookmarkUUID"];
				
				
				// END Setup a bookmarkDictionary
				
				
				// Add BM to File
				[[[[fullBMFile objectForKey:@"Children"] objectAtIndex:bmLocation] objectForKey:@"Children"] insertObject:[folderInfo mutableCopy] atIndex:0];
				
				
				// Write fullBMFile to file
				//NSString *wpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Safari/"];
				//NSString *wfinalPath = [wpath stringByAppendingPathComponent:@"Bookmarks_sBookmarkTool.plist"];
				
				if (! [fullBMFile writeToFile:wfinalPath atomically: YES]) {
					printf("\tERROR: Bookmark was not added to the Bookmark Bar.\n\n");	
					exit (1);
				}
				
				// release all the objects
				[folderInfo release];
	
				
			// Only 1 Folder was found with that name
			} else if (folderExists == 1) {
				
				//NSLog(@"Folder at Index %s was found with that name.", [[NSString stringWithFormat:@"%i", folderExistsIndex] UTF8String]);
				
				//NSLog(@"This is the contents of bm %@:\n%@", folderTitle, [currentBMContents objectAtIndex:1]);
				
			
				
				// START Setup a bookmarkDictionary
				
				// Get the Main folder Dictionary from plist
				NSMutableDictionary *folderInfo = [[NSMutableDictionary alloc] initWithDictionary:[currentBMContents objectAtIndex:folderExistsIndex]];
				
				
				// Add Children Array to BM Dictionary
				NSMutableArray *theChildren = [[NSMutableArray alloc] initWithArray:[[folderInfo objectForKey:@"Children"] copy]];
				
				// START Setup a bookmarkDictionary
				
				// Setup Main BM Dictionary
				NSMutableDictionary *bmInfo = [[NSMutableDictionary alloc] init];
				
				//Setup BM Title Dictionary
				NSMutableDictionary *bmTempTitle = [[NSMutableDictionary alloc] init];
				[bmTempTitle setObject:[NSString stringWithFormat:@"%@",bmTitle] forKey: titleKeyName];
				
				// Add title to bm Dictionary
				[bmInfo setObject:bmTempTitle forKey:@"URIDictionary"];
				
				// Add URL to bm Dictionary
				[bmInfo setObject:[NSString stringWithFormat:@"%@",bmURL] forKey:@"URLString"];
				
				
				// Add WebBookmarkType String
				[bmInfo setObject:[NSString stringWithString:@"WebBookmarkTypeLeaf"] forKey:@"WebBookmarkType"];
				
				// Add UUID for the BM
				[bmInfo setObject:[NSString stringWithFormat:@"%@",[self stringWithUUID]] forKey:@"WebBookmarkUUID"];
				
				// END Setup a bookmarkDictionary

				
				
				// Add the URL to the children dictionary
				[theChildren insertObject:bmInfo atIndex:0];
				
								
				// END Setup a bookmarkDictionary
				
				
				// Add BM to File
				[[[[[fullBMFile objectForKey:@"Children"] objectAtIndex:bmLocation] objectForKey:@"Children"] objectAtIndex:folderExistsIndex] setObject:theChildren forKey:@"Children"];
				
				
				// Write fullBMFile to file
				//NSString *wpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Safari/"];
				//NSString *wfinalPath = [wpath stringByAppendingPathComponent:@"Bookmarks_sBookmarkTool.plist"];
				
				if (! [fullBMFile writeToFile:wfinalPath atomically: YES]) {
					printf("\tERROR: Bookmark was not added to the Bookmark Bar.\n\n");
										exit (1);
				}
				
				// release all the objects
				[folderInfo release];
				[bmInfo release];
				[theChildren release];
				[bmTempTitle release];
				
			
			// Several folders were found with that name
			} else {
				printf("\nERROR:\tFound %s folder(s) with the same name. No folder was created.\n", [[NSString stringWithFormat:@"%i", folderExists] UTF8String]);
			}

			// release all the objects
			[currentBMContents release];
			[fullBMFile release];
			[tempBarBM release];
			
		}
		
		
		} else if(strcasecmp([theLocation UTF8String], "Menu") == 0 ){
#pragma mark -	-	Create Bookmark in the Folder in the Menu
		
		//setup the barBookmarks array
		int bmLocation = [self findLocationIndex:[fullBMFile objectForKey:@"Children"] :[NSString stringWithString:@"BookmarksMenu"]];
		NSDictionary *tempMenuBM = [[NSDictionary alloc] initWithDictionary:[[fullBMFile objectForKey:@"Children"] objectAtIndex:bmLocation]];
		
		
		if ( strcmp([[tempMenuBM objectForKey:titleKeyName] UTF8String], "BookmarksMenu") == 0) {
			
			
			// Setup variable for found folder
			int folderExists;
			folderExists = 0;
			
			//Setup variable to find index for found folder
			int bmIndexer;
			int folderExistsIndex;
			bmIndexer = 0;
			
			
			// Test for existing folder
			NSMutableArray *currentBMContents = [[NSMutableArray alloc] initWithArray:[[tempMenuBM objectForKey:@"Children"] copy]];
			
			//NSLog(@"The Count is: %i", [currentBMContents count]);
			
			//Accessing an Array's contents using NSEnumerator
			NSEnumerator *enumerator = [currentBMContents objectEnumerator];
			id obj;
			
			while ( obj = [enumerator nextObject] ) {
				
				// Test for Folders of bookmarks
				if (strcmp([[obj objectForKey:@"WebBookmarkType"] UTF8String], "WebBookmarkTypeList") == 0){
					
					// Test for a folder with the same name as the one given
					if (strcmp([[obj objectForKey:titleKeyName] UTF8String], [folderTitle UTF8String]) == 0){
						
						// Folder exists add 1 to the amount of folders found
						folderExists++;
						
						// folder exists get the index of the folder
						folderExistsIndex = bmIndexer;
						
						//NSLog( @"%@", obj);
					}
				}
				
				// Add 1 to indexer
				bmIndexer++;
			}
			
			// No Folder found, create a new one
			if (folderExists == 0) {				
				
				// START Setup a bookmarkDictionary
				
				// Setup Main BM Dictionary
				NSMutableDictionary *folderInfo = [[NSMutableDictionary alloc] init];
				
				
				// Add Children Array to BM Dictionary
				NSMutableArray *theChildren = [[NSMutableArray alloc] init];
				
				
				// START Setup a bookmarkDictionary
				
				// Setup Main BM Dictionary
				NSMutableDictionary *bmInfo = [[NSMutableDictionary alloc] init];
				
				//Setup BM Title Dictionary
				NSMutableDictionary *bmTempTitle = [[NSMutableDictionary alloc] init];
				[bmTempTitle setObject:[NSString stringWithFormat:@"%@",bmTitle] forKey: titleKeyName];
				
				// Add title to bm Dictionary
				[bmInfo setObject:bmTempTitle forKey:@"URIDictionary"];
				
				// Add URL to bm Dictionary
				[bmInfo setObject:[NSString stringWithFormat:@"%@",bmURL] forKey:@"URLString"];
				
				
				// Add WebBookmarkType String
				[bmInfo setObject:[NSString stringWithString:@"WebBookmarkTypeLeaf"] forKey:@"WebBookmarkType"];
				
				// Add UUID for the BM
				[bmInfo setObject:[NSString stringWithFormat:@"%@",[self stringWithUUID]] forKey:@"WebBookmarkUUID"];
				
				// END Setup a bookmarkDictionary
				
				// Add the URL to the children dictionary
				[theChildren insertObject:bmInfo atIndex:0];
				
				// Add the children array to the folder info
				[folderInfo setObject:theChildren forKey:@"Children"];
				
				// Add URL to bm Dictionary
				[folderInfo setObject:[NSString stringWithFormat:@"%@",folderTitle] forKey:titleKeyName];
				
				
				// Add WebBookmarkType String
				[folderInfo setObject:[NSString stringWithString:@"WebBookmarkTypeList"] forKey:@"WebBookmarkType"];
				
				// Add UUID for the BM
				[folderInfo setObject:[NSString stringWithFormat:@"%@",[self stringWithUUID]] forKey:@"WebBookmarkUUID"];
				
				
				// END Setup a bookmarkDictionary
				
				
				// Add BM to File
				[[[[fullBMFile objectForKey:@"Children"] objectAtIndex:bmLocation] objectForKey:@"Children"] insertObject:[folderInfo mutableCopy] atIndex:0];
				
				
				// Write fullBMFile to file
				//NSString *wpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Safari/"];
				//NSString *wfinalPath = [wpath stringByAppendingPathComponent:@"Bookmarks_sBookmarkTool.plist"];
				
				if (! [fullBMFile writeToFile:wfinalPath atomically: YES]) {
					printf("\tERROR: Bookmark was not added to the Bookmark Bar.\n\n");	
										exit (1);
				}
				
				// release all the objects
				[folderInfo release];
				
				
				// Only 1 Folder was found with that name
			} else if (folderExists == 1) {
				
				//NSLog(@"Folder at Index %s was found with that name.", [[NSString stringWithFormat:@"%i", folderExistsIndex] UTF8String]);
				
				//NSLog(@"This is the contents of bm %@:\n%@", folderTitle, [currentBMContents objectAtIndex:1]);
				
				
				
				// START Setup a bookmarkDictionary
				
				// Get the Main folder Dictionary from plist
				NSMutableDictionary *folderInfo = [[NSMutableDictionary alloc] initWithDictionary:[currentBMContents objectAtIndex:folderExistsIndex]];
				
				
				// Add Children Array to BM Dictionary
				NSMutableArray *theChildren = [[NSMutableArray alloc] initWithArray:[[folderInfo objectForKey:@"Children"] copy]];
				
				// START Setup a bookmarkDictionary
				
				// Setup Main BM Dictionary
				NSMutableDictionary *bmInfo = [[NSMutableDictionary alloc] init];
				
				//Setup BM Title Dictionary
				NSMutableDictionary *bmTempTitle = [[NSMutableDictionary alloc] init];
				[bmTempTitle setObject:[NSString stringWithFormat:@"%@",bmTitle] forKey: titleKeyName];
				
				// Add title to bm Dictionary
				[bmInfo setObject:bmTempTitle forKey:@"URIDictionary"];
				
				// Add URL to bm Dictionary
				[bmInfo setObject:[NSString stringWithFormat:@"%@",bmURL] forKey:@"URLString"];
				
				
				// Add WebBookmarkType String
				[bmInfo setObject:[NSString stringWithString:@"WebBookmarkTypeLeaf"] forKey:@"WebBookmarkType"];
				
				// Add UUID for the BM
				[bmInfo setObject:[NSString stringWithFormat:@"%@",[self stringWithUUID]] forKey:@"WebBookmarkUUID"];
				
				// END Setup a bookmarkDictionary
				
				
				
				// Add the URL to the children dictionary
				[theChildren insertObject:bmInfo atIndex:0];
				
				
				// END Setup a bookmarkDictionary
				
				
				// Add BM to File
				[[[[[fullBMFile objectForKey:@"Children"] objectAtIndex:bmLocation] objectForKey:@"Children"] objectAtIndex:folderExistsIndex] setObject:theChildren forKey:@"Children"];
				
				
				// Write fullBMFile to file
				//NSString *wpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Safari/"];
				//NSString *wfinalPath = [wpath stringByAppendingPathComponent:@"Bookmarks_sBookmarkTool.plist"];
				
				if (! [fullBMFile writeToFile:wfinalPath atomically: YES]) {
					printf("\tERROR: Bookmark was not added to the Bookmark Bar.\n\n");	
										exit (1);
				}
				
				// release all the objects
				[folderInfo release];
				[bmInfo release];
				[theChildren release];
				[bmTempTitle release];
				
				
				// Several folders were found with that name
			} else {
				printf("\nERROR:\tFound %s folder(s) with the same name. No folder was created.\n", [[NSString stringWithFormat:@"%i", folderExists] UTF8String]);
			}
			
			// release all the objects
			[currentBMContents release];
			[fullBMFile release];
			[tempMenuBM release];
			
		}
		
	} 
	
	
	// really done
	
}

// END Create single bookmark in the given folder at the given location

@end



#pragma mark - Start Program Code

/// Start Program Code HERE
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	
	//COPIED CODE INSERTED HERE
	
	
	NSMutableArray *args = [[NSMutableArray alloc] initWithArray:[[NSProcessInfo processInfo] arguments]];
	
	
	
	// BM Single Options Array
	NSMutableArray *singleOptions = [[NSMutableArray alloc] init];
	
	// BM Folder Options Array
	NSMutableArray *folderOptions = [[NSMutableArray alloc] init];
	
	// BM Group Options Array
	NSMutableArray *groupOptions = [[NSMutableArray alloc] init];
	
		// No Arguments were passed
		if ([args count] < 2 ){
			[args addObject:[NSString stringWithFormat:@"-h"]];
		}
		
		//Only get the first argument
		NSArray *singleArg = [[NSArray alloc] initWithObjects:[args objectAtIndex:1],nil];
		
		//NSCountedSet *cset = [[NSCountedSet alloc] initWithArray:args];
		NSCountedSet *cset = [[NSCountedSet alloc] initWithArray:singleArg];
		
		
		
		NSArray *sorted_args = [[cset allObjects]
								sortedArrayUsingSelector:@selector(compare:)];
		NSEnumerator *enm = [sorted_args objectEnumerator];
		id word;
		
		enum MirrorMode {
			help,
			deleteBar,
			deleteBoth,
			deleteMenu,
			createSingle,
			makeFolder,
			createInFolder
		} mode;
		
		mode = help;
		
		// Counter to get enumeration index
		int enmCounter = 0;
		
		while (word = [enm nextObject]) {
			//printf("%s\n", [word UTF8String]);
			if (strcmp([word UTF8String], "-h")==0){
				mode = help;
				break;
			}
			if (strcmp([word UTF8String], "-d")==0){
				
				if ([args count] == 3)
				{
					//Either Menu or Bar was supplied as extra options
					if (strcasecmp([[args objectAtIndex:2] UTF8String], "Menu") == 0) {
						mode = deleteMenu;
						break;
					} else if (strcasecmp([[args objectAtIndex:2] UTF8String], "Bar") == 0) {
						mode = deleteBar;
						break;
					} else if (strcasecmp([[args objectAtIndex:2] UTF8String], "Both") == 0) {
						mode= deleteBoth;
						break;
					}
					
				}
				
				//Error
				printf("ERROR: Wrong Syntax for -d, please see help.\n");
				break;
			}
			if (strcmp([word UTF8String], "-s") == 0){
				if ([args count] == 5)
				{
					
					
					//NSArray *tempSingleOptions = [[NSArray alloc] initWithObjects:[args objectAtIndex:2],[args objectAtIndex:3],[args objectAtIndex:4],nil];
					[singleOptions addObject:[args objectAtIndex:2]];
					[singleOptions addObject:[args objectAtIndex:3]];
					[singleOptions addObject:[args objectAtIndex:4]];
					mode = createSingle;
				} else {
					printf("ERROR: Please check syntax.\n\n");
				}
				break;
			}
			if (strcmp([word UTF8String], "-f")==0){
				
				if ([args count] == 4) {
					if (strcasecmp([[args objectAtIndex:2] UTF8String], "Bar") == 0) {
						[folderOptions addObject:[args objectAtIndex:2]];
						[folderOptions addObject:[args objectAtIndex:3]];
						
						mode = makeFolder;
					} else if (strcasecmp([[args objectAtIndex:2] UTF8String], "Menu") == 0) {
						[folderOptions addObject:[args objectAtIndex:2]];
						[folderOptions addObject:[args objectAtIndex:3]];
						mode = makeFolder;	
					}
				}
				break;
			}
			if (strcmp([word UTF8String], "-g")==0){
				
				if ([args count] == 6) {
					if (strcasecmp([[args objectAtIndex:2] UTF8String], "Bar") == 0) {
						[groupOptions addObject:[args objectAtIndex:2]];
						[groupOptions addObject:[args objectAtIndex:3]];
						[groupOptions addObject:[args objectAtIndex:4]];
						[groupOptions addObject:[args objectAtIndex:5]];
						mode = createInFolder;
						
					} else if (strcasecmp([[args objectAtIndex:2] UTF8String], "Menu") == 0) {
						[groupOptions addObject:[args objectAtIndex:2]];
						[groupOptions addObject:[args objectAtIndex:3]];
						[groupOptions addObject:[args objectAtIndex:4]];
						[groupOptions addObject:[args objectAtIndex:5]];					
						mode = createInFolder;
					}
				}
				break;
			}		
			//add one to counter
			enmCounter = enmCounter + 1;
			//NSLog(@"enmCounter is: %i", enmCounter);
			
			
		}
		[cset release];
		
		// Mode is Help or no command line argument given
		if (mode == help){
			printf("     ____              _     __  __            _      _______          _ \n");
			printf("    |  _ \\            | |   |  \\/  |          | |    |__   __|        | |\n");
			printf(" ___| |_) | ___   ___ | | __| \\  / | __ _ _ __| | __    | | ___   ___ | |\n");
			printf("/ __|  _ < / _ \\ / _ \\| |/ /| |\\/| |/ _` | '__| |/ /    | |/ _ \\ / _ \\| |\n");
			printf("\\__ \\ |_) | (_) | (_) |   < | |  | | (_| | |  |   <     | | (_) | (_) | |\n");
			printf("|___/____/ \\___/ \\___/|_|\\_\\|_|  |_|\\__,_|_|  |_|\\_\\    |_|\\___/ \\___/|_|\n");
			printf("\n");
			printf("\nsBookmarkTool version 1.01\nCopyright 2010, Dany FloresBravo\n");
			printf("===============================================================================================================\n\n\n\n");
			printf("usage: sBookmarkTool [option]\tPassing more than one option produces undefined behavior.\n\n");
			printf("\n...............................................................................................................\n");
			printf("\n  -h\t\t\t\bPrint this usage and exit.\n");
			printf("\n...............................................................................................................\n\n\n");			
			printf("\n  -d <location>\t\tRemove all bookmaks from the specified location <location>");
			printf("\n\t\t\t\t<location>  can be one of these choices:");
			printf("\n\t\t\t\t\tBar\tBookmarks Bar");
			printf("\n\t\t\t\t\tMenu\tBookmarks Menu");
			printf("\n\t\t\t\t\tBoth\tBookmarks Bar and Bookmarks Menu");
			
			printf("\n...............................................................................................................\n\n\n");
			printf("\n  -s <location> <title> <url>\n\t\t\tCreate a single bookmark in the location <location>\n\t\t\twith the title <title> and the url <url>");
			printf("\n\t\t\t\t<location>\t\tcan be either Bar or Menu.");
			printf("\n\t\t\t\t<title>\t\t\tcan be any string to identify your bookmark.");
			printf("\n\t\t\t\t<url>\t\t\tis a a string with the url address of your bookmark.\n");
			printf("\n...............................................................................................................\n\n\n");			
			printf("\n  -f <location> <title>\n\t\t\tCreate a an empty bookmark folder in the location <location>\n\t\t\twith the name <title>");
			printf("\n\t\t\t\t<location>\tcan be either Bar or Menu.");
			printf("\n\t\t\t\t<title>\t\tcan be any string to identify your bookmark folder.\n");
			printf("\n...............................................................................................................\n\n\n");			
			printf("\n  -g <location> <folderTitle> <urlTitle> <url>\n\t\t\tCreate a single bookmark in the location <location>\n\t\t\tin the folder <folderTitle> with the title <urlTitle>\n\t\t\twith the url address of <url>.\n\n\t\t\tIf a folder with the name <folderTitle> already exists, your new bookmark\n\t\t\twill be created and placed at the top of the list of current bookmarks.\n\n\t\t\tIf a folder with the name <folderTitle> doesn't exist, then it will be\n\t\t\tcreated and placed at the begining of the bookmark items in the\n\t\t\tlocation <location>. Then a new bookmark with the bookmark\n\t\t\tname <urlTitle> will be created in the folder.\n");
			printf("\n\t\t\t\t<location>\t\tcan be either Bar or Menu.");
			printf("\n\t\t\t\t<folderTitle>\t\tcan be any string to identify your bookmark folder.");
			printf("\n\t\t\t\t<urlTitle>\t\tcan be any string to identify your bookmark.");
			printf("\n\t\t\t\t<url>\t\t\tis a a string with the url address of your bookmark.\n");				
			
			
			
			printf("\n");
			return 0;
		}
		
		
		// Start implmentation of sBT object
		id objSBT;
		objSBT = [[sBT alloc] init];
		
	
	
		// Command line argument given other than help
		switch (mode) {
				
			case deleteBar:
				// Delete bm from bar
				[objSBT deleteAllBookmarksFrom:@"Bar"];
				
				break;
			case deleteBoth:
				// Delete bm from bar and menu
				[objSBT deleteAllBookmarksFrom:@"Both"];			
				break;
				
			case deleteMenu:
				// Delete bm from menu
				[objSBT deleteAllBookmarksFrom:@"Menu"];			
				break;
				
			case createSingle:
				//NSLog(@"I will run: %@", mode);
				[objSBT createSingleBookmarkAt:[NSString stringWithFormat:@"%@",[singleOptions objectAtIndex:0]] :[NSString stringWithFormat:@"%@",[singleOptions objectAtIndex:1]] :[NSString stringWithFormat:@"%@",[singleOptions objectAtIndex:2]]];
				break;
				
			case makeFolder:
				//NSLog(@"I will run: %@", mode);
				[objSBT createFolder:[folderOptions objectAtIndex:0]:[folderOptions objectAtIndex:1]];
				break;
				
			case createInFolder:
				[objSBT createBookmarkInFolder:[groupOptions objectAtIndex:0] :[groupOptions objectAtIndex:1] :[groupOptions objectAtIndex:2] :[groupOptions objectAtIndex:3]];
				break;
				
			default:
				break;
		}
		
		
		
		//END COPIED CODE

	
	// End here
    [pool drain];
    return 0;
}